package com.xjgzinfo.usdp.web.rest;

import com.xjgzinfo.usdp.core.domain.PageWapper;
import com.xjgzinfo.usdp.core.domain.ResponsePage;
import com.xjgzinfo.usdp.core.domain.ResponseResult;
import com.xjgzinfo.usdp.service.BladeVisualRecordQueryService;
import com.xjgzinfo.usdp.service.BladeVisualRecordService;
import com.xjgzinfo.usdp.service.dto.BladeVisualRecordCriteria;
import com.xjgzinfo.usdp.service.dto.BladeVisualRecordDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URISyntaxException;
import java.util.Optional;

/**
 * REST controller for managing {@link com.xjgzinfo.usdp.domain.BladeVisualRecord}.
 */
@RestController
@RequestMapping("/blade-visual")
public class RecordResource {

    private static final String ENTITY_NAME = "AvueServer4jAppBladeVisualRecord";
    private final Logger log = LoggerFactory.getLogger(RecordResource.class);
    private final BladeVisualRecordService bladeVisualRecordService;
    private final BladeVisualRecordQueryService bladeVisualRecordQueryService;
    @Value("${spring.application.name:usdp-app-unkown}")
    private String applicationName;

    public RecordResource(BladeVisualRecordService bladeVisualRecordService, BladeVisualRecordQueryService bladeVisualRecordQueryService) {
        this.bladeVisualRecordService = bladeVisualRecordService;
        this.bladeVisualRecordQueryService = bladeVisualRecordQueryService;
    }

    /**
     * {@code POST  /record/save} : 创建实体 bladeVisualRecord.
     *
     * @param bladeVisualRecordDTO the bladeVisualRecordDTO to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new bladeVisualRecordDTO, or with status {@code 400 (Bad Request)} if the bladeVisualRecord has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/record/save")
    public ResponseResult<BladeVisualRecordDTO> createBladeVisualRecord(@RequestBody BladeVisualRecordDTO bladeVisualRecordDTO) throws URISyntaxException {
        log.debug("REST request to save BladeVisualRecord : {}", bladeVisualRecordDTO);
        if (bladeVisualRecordDTO.getId() != null) {
            return new ResponseResult(200, null, "ID已存在", false);
        }
        BladeVisualRecordDTO result = bladeVisualRecordService.save(bladeVisualRecordDTO);
        if (result.getId() != null) {
            return new ResponseResult(200, result, "创建成功", true);
        } else {
            return new ResponseResult(400, result, "创建失败", false);
        }
    }

    /**
     * {@code PUT  /record/update} : 修改实体 bladeVisualRecord.
     *
     * @param bladeVisualRecordDTO the bladeVisualRecordDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated bladeVisualRecordDTO,
     * or with status {@code 400 (Bad Request)} if the bladeVisualRecordDTO is not valid,
     * or with status {@code 500 (Internal Server Error)} if the bladeVisualRecordDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/record/update")
    public ResponseResult<BladeVisualRecordDTO> updateBladeVisualRecord(@RequestBody BladeVisualRecordDTO bladeVisualRecordDTO) throws URISyntaxException {
        log.debug("REST request to update BladeVisualRecord : {}", bladeVisualRecordDTO);
        if (bladeVisualRecordDTO.getId() == null) {
            new ResponseResult(200, null, "ID不能为空", false);
        }
        BladeVisualRecordDTO result = bladeVisualRecordService.save(bladeVisualRecordDTO);
        return new ResponseResult(200, result, "修改成功", true);
    }

    /**
     * {@code GET  /record/list} : 分页获取实体列表 bladeVisualRecords.
     *
     * @param pageable the pagination information.
     * @param criteria the criteria which the requested entities should match.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of bladeVisualRecords in body.
     */
    @GetMapping("/record/list")
    public ResponsePage<BladeVisualRecordDTO> getAllBladeVisualRecords(BladeVisualRecordCriteria criteria, Pageable pageable) {
        log.debug("REST request to get BladeVisualRecords by criteria: {}", criteria);
        Page<BladeVisualRecordDTO> page = bladeVisualRecordQueryService.findByCriteria(criteria, pageable);
        PageWapper<BladeVisualRecordDTO> pageWapper = new PageWapper(page.getNumber(), page.getTotalPages(), page.getContent(), page.getSize(), page.getTotalElements());
        return new ResponsePage(200, pageWapper, "", true);
    }

    /**
     * {@code GET  /record/detail} : ID查询实体 bladeVisualRecord.
     *
     * @param id the id of the bladeVisualRecordDTO to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the bladeVisualRecordDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/record/detail")
    public ResponseResult<BladeVisualRecordDTO> getBladeVisualRecord(@RequestParam(value = "id", required = false) Long id, @RequestParam(value = "id.equals", required = false) Long equalsId) {
        long queryId = id != null ? id : equalsId;
        log.debug("REST request to get BladeVisualRecord : {} ", queryId);
        Optional<BladeVisualRecordDTO> bladeVisualRecordDTO = bladeVisualRecordService.findOne(queryId);
        if (bladeVisualRecordDTO.isPresent()) {
            return new ResponseResult(200, bladeVisualRecordDTO.get(), "", true);
        } else {
            return new ResponseResult(404, null, "未查询到数据", false);
        }
    }

    /**
     * {@code DELETE  /record/remove} : ID删除实体 bladeVisualRecord.
     *
     * @param ids the id of the bladeVisualRecordDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @PostMapping("/record/remove")
    public ResponseResult<Void> deleteBladeVisualRecord(@RequestParam(value = "ids", required = false) Long ids, @RequestParam(value = "id.equals", required = false) Long equalsId) {
        long deleteId = ids != null ? ids : equalsId;
        log.debug("REST request to delete BladeVisualRecord : {} ", deleteId);
        bladeVisualRecordService.delete(deleteId);
        return new ResponseResult(200, null, "删除成功", true);
//        Optional<BladeVisualRecordDTO> bladeVisualRecordDTOOpt = bladeVisualRecordService.findOne(deleteId);
//        if (bladeVisualRecordDTOOpt.isPresent()) {
//            BladeVisualRecordDTO bladeVisualRecordDTO = bladeVisualRecordDTOOpt.get();
//            if (bladeVisualRecordDTO.getIsDeleted()==null) {
//                bladeVisualRecordDTO.setIsDeleted(1);
//                bladeVisualRecordService.save(bladeVisualRecordDTO);
//            } else {
//                bladeVisualRecordService.delete(deleteId);
//            }
//            return new ResponseResult(404, null, "删除成功", true);
//        } else {
//            return new ResponseResult(404, null, "未查询到数据", false);
//        }
    }
}
